# @Time:2021/6/9 15:33
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import pygame
import os


# 对actor相同的部分进行封装

class DirAction(object):
    def __init__(self, path_name1: str, path_name2: str, prefix: str, dir_count: int, frame_count: int, is_loop: bool):
        '''
        行为动画效果
        :param path_name1: 最上一层路径文件夹名
        :param path_name2:本层路径文件夹名
        :param prefix:文件名前缀
        :param dir_count:方向的数量
        :param frame_count:一个方向上图片的数量
        :param is_loop:是否循环显示
        '''
        self.path_name = path_name1
        self.image_index = 0
        self.action_images = []
        self.frame_count = frame_count
        self.dir = 0
        self.is_loop = is_loop

        for j in range(0, dir_count):  # 有几个方向
            dir_image = []  # 存储每个方向上的图片
            for i in range(0, frame_count):  # 一个方向上有几张图
                img_path = os.path.join('../resource', 'img',
                                        path_name1, path_name2,
                                        prefix + str.format("%02d" % j) + str.format("%03d" % i) + '.tga')
                dir_image.append(pygame.image.load(img_path))
            self.action_images.append(dir_image)  # 存储所有方向上的图片，每个方向的图片都是一个列表

    def get_curr_frame(self, dir):
        current_img = self.action_images[dir][self.image_index]
        if self.image_index + 1 >= self.frame_count:
            if self.is_loop:
                self.image_index = 0
        else:
            self.image_index += 1
        return current_img

    def is_end(self):  # 判断帧图像是否走到最后
        if self.is_loop:
            return False
        else:
            if self.image_index >= self.frame_count - 1:
                return True
            else:
                return False

    def reset(self):
        self.image_index = 0
